package org.xtown.bingluo.xtieba.ui.activities;

import org.xtown.bingluo.xtieba.R;
import org.xtown.bingluo.xtieba.appinfo.SettingInfo;
import org.xtown.bingluo.xtieba.utils.UiUtils;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;

public class SettingActivity extends PreferenceActivity implements
		OnPreferenceChangeListener, OnPreferenceClickListener {

	private CheckBoxPreference m_autohorscreemPref;
	private Preference m_checkupdatePref;
	private Preference m_aboutPref;
	private ListPreference m_page_sizePref;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 从xml文件中添加Preference项
		addPreferencesFromResource(R.xml.setting_preferences);
		SettingInfo.setSettingInfo(this);
		myFindView();
	}

	public void myFindView() {
		m_autohorscreemPref = (CheckBoxPreference) findPreference(SettingInfo.m_autohorscreem_key);
		m_page_sizePref = (ListPreference) findPreference(SettingInfo.m_page_size_key);
		m_checkupdatePref = (Preference) findPreference(SettingInfo.m_checkupdate_key);
		m_aboutPref = (Preference) findPreference(SettingInfo.m_about_key);
		// 为各个Preference注册监听接口
		m_autohorscreemPref.setOnPreferenceChangeListener(this);
		m_page_sizePref.setOnPreferenceChangeListener(this);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}
	
	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		// 判断是哪个Preference改变了
		if (preference.getKey().equals(SettingInfo.m_autohorscreem_key)) {
			SettingInfo.m_autohorscreem_v = (Boolean)newValue;
			//屏幕设置
			UiUtils.settingScreenOrientation(this, SettingInfo.m_autohorscreem_v);
		} else if (preference.getKey().equals(SettingInfo.m_page_size_key)) {
			SettingInfo.m_page_size_v = (String)newValue;
		} else {
			// 如果返回false表示不允许被改变
			return false;
		}
		// 返回true表示允许改变
		return true;
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		Log.v("SystemSetting", "preference is clicked");
		Log.v("Key_SystemSetting", preference.getKey());
		// 判断是哪个Preference被点击了
		if (preference.getKey().equals(SettingInfo.m_checkupdate_key)) {
			Log.v("SystemSetting", "m_checkupdate_key preference is clicked");
		} else if (preference.getKey().equals(SettingInfo.m_about_key)) {
			Log.v("SystemSetting", "m_about_key preference is clicked");
		} else {
			return false;
		}
		return true;
	}

	@Override
	public boolean onPreferenceTreeClick(
			PreferenceScreen paramPreferenceScreen, Preference paramPreference) {
		
		if (paramPreference == this.m_checkupdatePref) {

		} else if (paramPreference == this.m_aboutPref) {
			showDialog(1);
		}
		return false;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Builder builder = null;
		switch (id) {
		case 1:
			builder = new AlertDialog.Builder(this)
//					.setIcon(R.drawable.)
					.setTitle(getResources().getString(R.string.aboutus) + " " + getResources().getString(R.string.ApplicationName))
					.setMessage(String.format(getResources().getString(R.string.aboutmessage), getResources().getString(R.string.ApplicationName), "1.0.0", "2012-10").toString());
			break;
		case 2:
			
			break;
		default:
			break;
		}
		return builder.create();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		System.out.println("SettingonResume");
		//屏幕设置
		UiUtils.settingScreenOrientation(this, SettingInfo.m_autohorscreem_v);
	}
}
